﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyLifeInAMinute.SharePoint.SharePointSolutionDownloader
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.btnBrowse.Focus();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = this.folderBrowserDLoc.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.txtDLoc.Text = this.folderBrowserDLoc.SelectedPath;
            }
            else
            {
                this.txtDLoc.Text = string.Empty;
            }

            this.btnSave.Visible = (this.txtDLoc.Text.Length > 0);
        }

        private void GetSolutions()
        {
            Microsoft.SharePoint.Administration.SPSolutionCollection spSolutions = Microsoft.SharePoint.Administration.SPFarm.Local.Solutions;
            SPSolutionCollection collection = new SPSolutionCollection();
            foreach (Microsoft.SharePoint.Administration.SPSolution spSolution in spSolutions)
            {
                Microsoft.SharePoint.Administration.SPPersistedFile file = spSolution.SolutionFile;

                SPSolution solution = new SPSolution(file.Id, file.DisplayName, file.Name, file.Status.ToString());

                collection.Add(solution);
            }

            this.sPSolutionCollectionBindingSource.DataSource = collection;
            this.dgSolutions.Focus();

            this.pnlSave.Visible = (this.dgSolutions.Rows.Count > 0);
        }

        private void btnGetSolutions_Click(object sender, EventArgs e)
        {
            this.GetSolutions();
        }

        private void tsFileRetrieveSolutions_Click(object sender, EventArgs e)
        {
            this.GetSolutions();
        }

        private void tsFileExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int selCount = this.dgSolutions.SelectedRows.Count;

            if (selCount == 0)
            {
                MessageBox.Show("You have not selected any solutions.", "No Solutions Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                this.btnSave.Enabled = false;

                Microsoft.SharePoint.Administration.SPSolutionCollection spSolutions = Microsoft.SharePoint.Administration.SPFarm.Local.Solutions;
                foreach (Microsoft.SharePoint.Administration.SPSolution spSolution in spSolutions)
                {
                    foreach (DataGridViewRow row in this.dgSolutions.SelectedRows)
                    {
                        Guid currentId = new Guid(row.Cells["Id"].Value.ToString());
                        if (spSolution.SolutionFile.Id == currentId)
                        {
                            Microsoft.SharePoint.Administration.SPPersistedFile spFile = spSolution.SolutionFile;
                            string fullFilePath = this.txtDLoc.Text + "\\" + spFile.Name;
                            if (System.IO.File.Exists(fullFilePath))
                                System.IO.File.Delete(fullFilePath);

                            spFile.SaveAs(fullFilePath);
                        }
                    }
                }

                MessageBox.Show("Saved " + selCount.ToString() + " solutions.", "Solutions Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.btnSave.Enabled = true;

                this.dgSolutions.ClearSelection();
            }
        }
    }
}
